/*
 * SoftwareSAR - 1.0
 */
package br.com.cpqd.ssar.mediador.gui;

import br.com.cpqd.ssar.utilitarias.Material;
import java.awt.Color;
import java.awt.Image;
import java.awt.Toolkit;
import javax.swing.JColorChooser;

/**
 * Janela de Criação de um Material
 * @author  Rafael Buck
 * @version 1.0
 */
public class JanelaCadastrarMaterial extends javax.swing.JDialog {

    /**
     * Controlador de Janelas
     */
    ControladorJanelas controladorJanelas;

    /**
     * Método Construtor
     * @param controladorJanelas ControladorJanelas
     * @param parent Frame
     * @param modal determina se a janela é modal ou não (impede o acesso as janelas abaixo)
     */
    public JanelaCadastrarMaterial(ControladorJanelas controladorJanelas, java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        this.controladorJanelas = controladorJanelas;
        initComponents();

        // Seta icone padrao
        Image img = Toolkit.getDefaultToolkit().getImage(controladorJanelas.obterCaminhoIcones());
        this.setIconImage(img);
    }

    /**
     * Tenta Aplicar as Propriedades Editadas
     */
    private Material tentaAplicarPropriedades() {
        boolean nenhumErro = true;
        String nome = null;
        double permissividade = 0, permeabilidade = 0, condutividade = 0, perdasMagneticas = 0, densidadeEspecifica = 0;
        Color cor = jPanelCor.getBackground();

        // Testa o nome
        try {
            nome = jTextFieldNomeMaterial.getText();
            // Não deve conter os seguintes Caracteres
            if (nome.contains(".") || nome.contains("\\")) {
                jTextFieldNomeMaterial.setForeground(Color.RED);
                nenhumErro = false;
            }
        } catch (Exception e) {
            jTextFieldNomeMaterial.setForeground(Color.RED);
            nenhumErro = false;
        }
        // Testa a permissividade
        try {
            permissividade = Double.valueOf(jTextFieldPermissividade.getText());
        } catch (Exception e) {
            jTextFieldPermissividade.setForeground(Color.RED);
            nenhumErro = false;
        }
        // Testa a permeabilidade
        try {
            permeabilidade = Double.valueOf(jTextFieldPermeabilidade.getText());
        } catch (Exception e) {
            jTextFieldPermeabilidade.setForeground(Color.RED);
            nenhumErro = false;
        }
        // Testa a condutividade
        try {
            condutividade = Double.valueOf(jTextFieldCondutividade.getText());
        } catch (Exception e) {
            jTextFieldCondutividade.setForeground(Color.RED);
            nenhumErro = false;
        }
        // Testa as perdas Magneticas
        try {
            perdasMagneticas = Double.valueOf(jTextFieldPerdasMagneticas.getText());
        } catch (Exception e) {
            jTextFieldPerdasMagneticas.setForeground(Color.RED);
            nenhumErro = false;
        }
        // Testa a densidade Especifica
        try {
            densidadeEspecifica = Double.valueOf(jTextFieldDensidadeEspecifica.getText());
        } catch (Exception e) {
            jTextFieldDensidadeEspecifica.setForeground(Color.RED);
            nenhumErro = false;
        }

        if (nenhumErro) {
            Material material = new Material(true);

            material.setNome(nome);
            material.setPermissividade(permissividade);
            material.setPermeabilidade(permeabilidade);
            material.setCondutividade(condutividade);
            material.setPerdaMagnetica(perdasMagneticas);
            material.setDensidadeEspecifica(densidadeEspecifica);
            material.setCor(cor);

            return material;
        } else {
//                Toolkit.getDefaultToolkit().beep();
            return null;
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldNomeMaterial = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jPanelCor = new javax.swing.JPanel();
        jButtonAlterarCor = new javax.swing.JButton();
        jTextFieldPermissividade = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jTextFieldPermeabilidade = new javax.swing.JTextField();
        jTextFieldCondutividade = new javax.swing.JTextField();
        jTextFieldPerdasMagneticas = new javax.swing.JTextField();
        jTextFieldDensidadeEspecifica = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jSeparator1 = new javax.swing.JSeparator();
        jButtonCancelar = new javax.swing.JButton();
        jButtonOK = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cadastrar Novo Material");
        setBackground(new java.awt.Color(231, 240, 250));
        setLocationByPlatform(true);
        setModal(true);
        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(231, 240, 250));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Propriedades Eletromagnéticas do Material"));

        jLabel2.setText("Nome: ");

        jTextFieldNomeMaterial.setText("novo_material");

        jLabel3.setText("Permissividade: ");

        jLabel4.setText("Permeabilidade: ");

        jLabel5.setText("Condutividade: ");

        jLabel6.setText("Perdas Magnéticas: ");

        jLabel7.setText("Densidade Específica: ");

        jPanelCor.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(102, 102, 102)));
        jPanelCor.setToolTipText("Cor do Material");

        javax.swing.GroupLayout jPanelCorLayout = new javax.swing.GroupLayout(jPanelCor);
        jPanelCor.setLayout(jPanelCorLayout);
        jPanelCorLayout.setHorizontalGroup(
            jPanelCorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 39, Short.MAX_VALUE)
        );
        jPanelCorLayout.setVerticalGroup(
            jPanelCorLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 19, Short.MAX_VALUE)
        );

        jButtonAlterarCor.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/cpqd/ssar/gerenciador/gui/recursos/color1.png"))); // NOI18N
        jButtonAlterarCor.setText("Cor");
        jButtonAlterarCor.setPreferredSize(new java.awt.Dimension(75, 23));
        jButtonAlterarCor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAlterarCorActionPerformed(evt);
            }
        });

        jLabel8.setText("[F/m]");

        jLabel9.setText("[H/m]");

        jLabel10.setText("[1/Ohm.m]");

        jLabel11.setText("[Tesla]");

        jLabel12.setText("[kg/m^3]");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextFieldNomeMaterial, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(35, 35, 35)
                .addComponent(jPanelCor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButtonAlterarCor, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 511, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel7))
                        .addGap(4, 4, 4)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextFieldDensidadeEspecifica, javax.swing.GroupLayout.DEFAULT_SIZE, 182, Short.MAX_VALUE)
                            .addComponent(jTextFieldPerdasMagneticas, javax.swing.GroupLayout.DEFAULT_SIZE, 182, Short.MAX_VALUE)))
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addGap(32, 32, 32)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextFieldCondutividade)
                            .addComponent(jTextFieldPermeabilidade)
                            .addComponent(jTextFieldPermissividade, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel9)
                            .addComponent(jLabel8)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(7, 7, 7)
                        .addComponent(jLabel10))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel11)
                            .addComponent(jLabel12))))
                .addGap(149, 149, 149))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(14, 14, 14)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanelCor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(jTextFieldNomeMaterial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jButtonAlterarCor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextFieldPermissividade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldPermeabilidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel9))
                .addGap(9, 9, 9)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldCondutividade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(jLabel10))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextFieldPerdasMagneticas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6)
                            .addComponent(jLabel11))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(jTextFieldDensidadeEspecifica, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addComponent(jLabel12)))
                .addGap(138, 138, 138))
        );

        jPanel2.setBackground(new java.awt.Color(231, 240, 250));
        jPanel2.setPreferredSize(new java.awt.Dimension(381, 300));

        jButtonCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/cpqd/ssar/gerenciador/gui/recursos/Cancelar.png"))); // NOI18N
        jButtonCancelar.setText("Cancelar");
        jButtonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelarActionPerformed(evt);
            }
        });

        jButtonOK.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/cpqd/ssar/gerenciador/gui/recursos/confirmar.png"))); // NOI18N
        jButtonOK.setText("Confirmar");
        jButtonOK.setPreferredSize(new java.awt.Dimension(75, 23));
        jButtonOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOKActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 182, Short.MAX_VALUE)
                        .addGap(85, 85, 85))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jButtonOK, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)))
                .addComponent(jButtonCancelar)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(31, 31, 31))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonCancelar)
                    .addComponent(jButtonOK, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 382, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 382, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButtonAlterarCorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAlterarCorActionPerformed
// TODO add your handling code here:
    JColorChooser jColorChooser = new JColorChooser();
    jPanelCor.setBackground(JColorChooser.showDialog(null, "Escolher uma Cor", Color.BLACK));
}//GEN-LAST:event_jButtonAlterarCorActionPerformed

private void jButtonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelarActionPerformed
// TODO add your handling code here:
    dispose();
}//GEN-LAST:event_jButtonCancelarActionPerformed

private void jButtonOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOKActionPerformed
// TODO add your handling code here:
    Material material = this.tentaAplicarPropriedades();
    if (material == null) {
    } else {
        if (controladorJanelas.adicionarMaterial(material)) {
            dispose();
        } else {
            jTextFieldNomeMaterial.setForeground(Color.RED);
        }
    }
}//GEN-LAST:event_jButtonOKActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAlterarCor;
    private javax.swing.JButton jButtonCancelar;
    private javax.swing.JButton jButtonOK;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanelCor;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JTextField jTextFieldCondutividade;
    private javax.swing.JTextField jTextFieldDensidadeEspecifica;
    private javax.swing.JTextField jTextFieldNomeMaterial;
    private javax.swing.JTextField jTextFieldPerdasMagneticas;
    private javax.swing.JTextField jTextFieldPermeabilidade;
    private javax.swing.JTextField jTextFieldPermissividade;
    // End of variables declaration//GEN-END:variables
}
